using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

using Geekeradmin.Authorize;
using Publicuse.Autofac;


using Publicuse.Interface;
using Publicuse.Util;
using Publicuse.Entity.AttributeUtil;
using Microsoft.Extensions.Options;
using Publicuse.Interface;
using Publicuse.Entity;
using Publicuse.Appication;
using Publicuse.Export;
using Publicuse.Interface.Extend;

namespace Geekeradmin.Controllers
{
    /// <summary>
    /// 数据表代码生成模板信息管理模块
    /// </summary>
    [UserAuthorize("Admin")]
    public class TbcodegenerationController : BaseController
    {
        public TbcodegenerationController(IAutofaceInstance autoface, IMySqlSugarService mySqlSugar, IWebHostEnvironment webHost,
            IOptions<DBConnectionOption> options, IBaseServiceExt baseServiceExt)
            : base(autoface, mySqlSugar, webHost, options, baseServiceExt)
        {
        }

        /// <summary>
        /// 获取数据表代码生成模板信息管理模块信息列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// 
        [HttpPost("GetList")]
        public async Task<JsonResult> GetList(Tbcodegeneration query)
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).GetList(query);
            return new JsonResult(result);
        }

        /// <summary>
        /// 检查数据表代码生成模板信息管理模块表名称是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("CheckName"), AllowAnonymous]
        public async Task<JsonResult> CheckName(string name, string loginUserCode, string Id = "")
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).CheckName(name, loginUserCode, Id);
            return new JsonResult(result);
        }

        /// <summary>
        /// 检查数据表代码生成模板信息管理模块表编码是否存在
        /// </summary>
        /// <param name="code"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("CheckCode"), AllowAnonymous]
        public async Task<JsonResult> CheckCode(string code, string loginUserCode, string Id = "")
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).CheckCode(code, loginUserCode, Id);
            return new JsonResult(result);
        }


        /// <summary>
        /// 新增修改数据表代码生成模板信息管理模块信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost("AddOrUpdate")]
        public async Task<JsonResult> AddOrUpdate(Tbcodegeneration info)
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).AddOrUpdate(info);
            return new JsonResult(result);
        }

        /// <summary>
        /// 数据表代码生成模板信息管理数据导入
        /// </summary>
        /// <returns></returns>
        [HttpPost("ImportExcel")]
        public async Task<JsonResult> ImportExcel()
        {
            string path = webHost.WebRootPath;
            var files = Request.Form.Files;
            string loginUserCode = Request.Form["loginUserCode"];
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).ImportExcel(files, path, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 批量审核数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("ConfirmAll"), AllowAnonymous]
        public async Task<JsonResult> ConfirmAll(string data, string loginUserCode)
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).ConfirmAll(data, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 审核数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("Confirm"), AllowAnonymous]
        public async Task<JsonResult> Confirm(string id, int status, string loginUserCode)
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).Confirm(id, status, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 删除数据表代码生成模板信息管理模块信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("Delete")]
        public async Task<JsonResult> Delete(string Id, string loginUserCode, string type = "-9")
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).Delete(Id, loginUserCode, type);
            return new JsonResult(result);
        }

        /// <summary>
        /// 还原数据表代码生成模板信息管理模块删除信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("Reduction"), AllowAnonymous]
        public async Task<JsonResult> Reduction(string Id, string loginUserCode)
        {
            CommonResult result = await TbcodegenerationApplication.GetApplication(container).Reduction(Id, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 导出数据信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet("ExportData")]
        public async Task<IActionResult> ExportData([FromQuery] Tbcodegeneration query)
        {
            return await InitExportData(webHost, mySqlSugar, query, "\\template\\report\\tbcodegeneration.xls",
            "数据表代码生成模板信息管理统计表", (a, b, c) =>
            {
                var ms = TbcodegenerationExport.ExportData(a, b, c, out var msgb);
                return ms;
            }, null, "", "", "", query.pageSize);
        }
    }
}